package cn.sinata.wanglin.neighbor.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.xilada.xldutils.activitys.BaseActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.TimeUtils;
import com.xilada.xldutils.utils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Circle;
import cn.sinata.wanglin.model.Image;
import cn.sinata.wanglin.model.Like;
import cn.sinata.wanglin.model.LikeData;
import cn.sinata.wanglin.neighbor.fragment.ShowImageFragment;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class ShowImagePagerActivity extends BaseActivity implements ViewPager.OnPageChangeListener{


    public static final String DATA = "data";
    public static final String POSITION = "position";
    @BindView(R.id.mViewPager)
    ViewPager mViewPager;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.action0)
    TextView action0;
    private int position = 0;
    private Circle circle;
    private ArrayList<String> urls = new ArrayList<>();
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_image_pager);
        ButterKnife.bind(this);

        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
        circle = (Circle) getIntent().getSerializableExtra(DATA);
        position = getIntent().getIntExtra(POSITION,0);
        if (circle == null) {
            finish();
            return;
        }

        List<Image> images = circle.getImages();
        if (images == null || images.size() == 0) {
            finish();
            return;
        }
        for (Image image : images) {
            urls.add(image.getUrl());
        }
        Utils.systemErr(position+"---urls->"+urls.size());
        tv_title.setText(String.format(Locale.CHINA,"%s\n%s/%s", TimeUtils.getTimeYYYYMMDDHHmm(circle.getTime()),position+1,urls.size()));
        tv_content.setMaxLines(7);
        tv_content.setEllipsize(TextUtils.TruncateAt.END);

        tv_content.setText(circle.getText());
        mViewPager.setAdapter(new ImagePagerAdapter(getSupportFragmentManager()));
        mViewPager.addOnPageChangeListener(this);
        mViewPager.setCurrentItem(position);

        action0.setText(circle.getClickPraise() == 1?"取消":"点赞");
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Utils.systemErr("onPageSelected--"+position+"---urls->"+urls.size());
        tv_title.setText(String.format(Locale.CHINA,"%s\n%s/%s", TimeUtils.getTimeYYYYMMDDHHmm(circle.getTime()),String.valueOf(position+1),String.valueOf(urls.size())));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private class ImagePagerAdapter extends FragmentStatePagerAdapter {
        public ImagePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return ShowImageFragment.getInstance(urls.get(position));
        }

        @Override
        public int getCount() {
            return urls.size();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mViewPager!=null) {
            mViewPager.removeOnPageChangeListener(this);
        }
    }

    private void praise(final Circle circle, String id){
        if (circle == null) {
            return;
        }
        showDialog();
        HttpManager.clickPraise(userId,"C",id)
                .subscribe(new ResultDataSubscriber<LikeData>(this) {
                    @Override
                    public void onSuccess(String msg, LikeData likeData) {
                        showToast(msg);
                        //当前用户行为 1：取消点赞 0：点赞
                        if (likeData.getClickPraise() == 0) {
                            int num = circle.getLikeNum();
                            if (num < 4) {
                                circle.getLikeList().add(likeData.getLike());
                            }
                            num++;
                            circle.setLikeNum(num);
                            circle.setClickPraise(1);
                        } else {
                            int num = circle.getLikeNum();
                            List<Like> likes = circle.getLikeList();
                            for (Like like : likes) {
                                if (like.getUserID().equals(userId)) {
                                    likes.remove(like);
                                    break;
                                }
                            }
                            num--;
                            if (num < 0) {
                                num = 0;
                            }
                            circle.setLikeNum(num);
                            circle.setClickPraise(0);
                        }
                        action0.setText(circle.getClickPraise() == 1?"取消":"点赞");
                    }
                });
    }

    @OnClick({R.id.tv_back,R.id.action0,R.id.tv_action1})
    void onViewClick(View v){
        switch (v.getId()) {
            case R.id.tv_back:
                finish();
            break;
            case R.id.action0:
                praise(circle,circle.getId());
                break;
            case R.id.tv_action1:
                ActivityUtil.create(this)
                        .go(CircleDetailActivity.class)
                        .put("type",1)
                        .put("id",circle.getId())
                        .start();
                break;
        }
    }
}
